package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.ImportAndExportSchoolsJdbc;
import com.wuseguang.springcloud.dao.jdbc.ImportAndExportSchoolsTimeJdbc;
import com.wuseguang.springcloud.service.ImportAndExportSchoolsService;
import com.wuseguang.springcloud.util.DateUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 学生进出校表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-03-29
 */
@Service
public class ImportAndExportSchoolsServiceImpl implements ImportAndExportSchoolsService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ImportAndExportSchoolsJdbc importAndExportSchoolsJdbc;
    @Autowired
    private ImportAndExportSchoolsTimeJdbc importAndExportSchoolsTimeJdbc;

    @Override
    public Result findStudentInAndOutByStudentId(Integer id) {

        //查询进出校信息
        String sql = "select * from (" +
                " select s.id,s.student_name,s.head_portrait,iaes.type,DATE_FORMAT(iaes.time,'%Y-%m-%d %T') time from student s " +
                " left join import_and_export_schools iaes on(s.id=iaes.student_id) where s.id =? ORDER BY iaes.id desc ) s GROUP BY s.id";
        List<Map<String,Object>> list =  importAndExportSchoolsJdbc.queryListMap(sql,id);

        //查询学生在学校的在校时长
        sql = "select `enter`,`leave`,total_time from `import_and_export_schools_time` where student_id   = ?";
        //学生在校时长
        Map<String,Object> timeMap = null;

        try{
            timeMap = importAndExportSchoolsTimeJdbc.queryMap(sql,id);
            timeMap.put("school_hours", DateUtil.getHour((String)list.get(0).get("time"),DateUtil.dateToString(new Date(),"yyyy-MM-dd HH:mm:ss")));
        }catch (Exception e){
            logger.info("没有数据");
            timeMap = new HashMap<>();
            timeMap.put("enter","0");
            timeMap.put("leave","0");
            timeMap.put("total_time","0.00");
            timeMap.put("school_hours", DateUtil.getHour((String)list.get(0).get("time"),DateUtil.dateToString(new Date(),"yyyy-MM-dd HH:mm:ss")));


        }

        Map<String,Object> returnMap = new HashMap<>();
        returnMap.put("list",list);
        returnMap.put("timeMap",timeMap);

        return new Result(returnMap);
    }
}
